// ## jm4bbx
#include "StreamWriterBox.h"
//#include "CellStreamExportVisitor.h"
//#include "../../runtime/AutomaticBoxCreatorRegistration.h"

//#include "../../common_bbx/tables/BoxDataTable.h"
//#include "../../common_bbx/sharedBoxData/BinaryOperationArguments.h"
// ## c++
#include <sstream>

namespace jm4bbx {

	const  groupType StreamWriterBox::INPUT_GROUP = groupType(0);
	const  columnType StreamWriterBox::INPUT_COLUMN = columnType(0);
	const inType StreamWriterBox::IN_ARC = inType(0);

	//const char* StreamWriterBox::OUTPUT_FORM_PARAM_NAME = "form";
	//const char* StreamWriterBox::FORM_PRINT_FIRST = "first";
	//const char* StreamWriterBox::FORM_PRINT_SECOND = "second";
	//const char* StreamWriterBox::FORM_PRINT_BOTH = "both";


	StreamWriterBox::StreamWriterBox(const BoxParamPack& pack) : RootBox(pack) {
		//format_ = getBoxParameterValue(pack, OUTPUT_FORM_PARAM_NAME, 
		//	std::string(FORM_PRINT_BOTH));
	}

	void StreamWriterBox::initImpl() {
		add_group(INPUT_GROUP, INPUT_COLUMN, get_input_descriptors(IN_ARC));

		wait4Receive();
		wait4SignalsIn();
	}

	//void StreamWriterBox::syncMachEtwas() {

	//	GroupPtr g = getGroupPtr(INPUT_GROUP);
	//	if (g->isPoisoned() || areSignalsInPoisoned()) {
	//		getOutputStream().close();
	//		//sendSignalsOut(true);
	//		wait4Receive();
	//		return;
	//	}

	//	BoxDataTable* r = BinaryOperationArguments<BoxDataTable>::getFirstArgument();
	//	BoxDataTable* s = BinaryOperationArguments<BoxDataTable>::getSecondArgument();

	//	getOutputStream().open();

	//	for (; g->getReadPosition() < g->getReadEnd(); 
	//		g->moveReadPosition()) {

	//			std::stringstream stream;

	//			BinaryOperationResultIndexType value = 
	//				get_data<BinaryOperationResultIndexPolicyType>(INPUT_COLUMN)[g->getReadPosition()];

	//			//stream << value;
	//			if (format_ == FORM_PRINT_FIRST) {						
	//				write(stream, r, value.firstTableRowIndex_);
	//			}
	//			else if (format_ == FORM_PRINT_SECOND) {
	//				write(stream, s, value.secondTableRowIndex_);
	//			}
	//			else if (format_ == FORM_PRINT_BOTH) {
	//				// namiesto konca riadku ", " a pokracuejem v zapise
	//				write(stream, r, value.firstTableRowIndex_, ",");						
	//				write(stream, s, value.secondTableRowIndex_);
	//			}
	//			// Naschval najskor do stringstreamu kt. je lokalna premenna
	//			// pretoze v pripade priameho dumpovania do outputStream_ by
	//			// mohlo dochadzat k castemu prekryvaniu zapisu pri paralelizacii. 
	//			getOutputStream() << stream.str();
	//	}

	////	
	//	wait4Receive();
	//}

	//std::ostream& StreamWriterBox::write(std::ostream& stream, BoxDataTable* t, 
	//	size_t row, const char* lineEnd /*= "\n"*/) {
	//		
	//		stream << t->getLine(row, lineEnd, ",");
	//		return stream;
	//}



} // namespace jm4bbx 
